//二级代码相关公用方法
/* eslint-disable no-unused-vars */

import clrstas from '/@/config/yinhai/clrStas'
import clrtype from '/@/config/yinhai/clrType'
import cnfmFlag from '/@/config/yinhai/cnfmFlag'
import area from '/@/config/area'
import common from '/@/common/common'

// 接口类型和方法
interface BaseType{
    readonly CLR_STAS:number

    readonly AREA:number

    readonly CLR_TYPE:number
    
    codeGetName(type: number,code: string): any

    transJsonCodeToName(arrayJson: Array<Object>, target:string, type: number): Array<Object>

}

class CodeHandler implements BaseType{

    /**
     * @param CLR_STAS number 清算状态
     */
    CLR_STAS:number
    /**
     * @param AREA number 统筹区划信息
     */
    AREA:number
    /**
     * @param CLR_TYPE number 一级清算类别
     */
    CLR_TYPE:number
    /**
     * @param CNFM_FLAG number 收款确认标志
     */
     CNFM_FLAG:number

    /**
     * @param data Object 二级代码集合
     */
    data: Object

    /**
     * 通过键获取二级代码的值
     * @param type number 二级代码类别
     * @param code string 二级代码的键
     * @returns 值
     */
    codeGetName(type: number,code: string):any {
        let typename = ''
        switch(type) {
            case this.CLR_STAS:
                typename = 'clrstas'
                break;
            case this.AREA:
                typename = 'area'
                break;
            case this.CLR_TYPE:
                typename = 'clrtype'
                break;
            case this.CNFM_FLAG:
                typename = 'cnfmFlag'
                break;
            default:
                return code
        }
        if(typename == 'area') {
            return area.getSingleInfo(code, area.AREA_NAME)
        }
        let array = this.data[typename] 
        for (let i in array) {
            if (array[i]['code'] == code) {
                return array[i]['name']
            }
        }
        return code
    }
    /**
     * 批量将Array里面的json特定字段，从键转换成值
     * @param arrayJson Array<Object> Array里面的json
     * @param target string Array里面需要转换的键值
     * @param type number 二级代码类别
     * @returns 值
     */
    transJsonCodeToName(arrayJson: Array<Object>, target:string, type: number): Array<Object> {
        let tempArray = JSON.parse(JSON.stringify(arrayJson))
        for (let i in tempArray) {
            if(common.objectAvailable(tempArray[i][target])){
                tempArray[i][target] = this.codeGetName(type, tempArray[i][target])
            }
        }
        return tempArray
    }
}

const codeHandler = new CodeHandler()
codeHandler.data = {}
codeHandler.data['clrstas'] = clrstas.data
codeHandler.data['clrtype'] = clrtype.data
codeHandler.data['cnfmFlag'] = cnfmFlag.data
codeHandler.CLR_STAS = 1
codeHandler.AREA = 2
codeHandler.CLR_TYPE = 3
codeHandler.CNFM_FLAG = 4
export default codeHandler